package com.example.myapp;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ProfileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ProfileFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<String> servicesList;
    private ServicesAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        // 初始化RecyclerView
        recyclerView = view.findViewById(R.id.servicesRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 设置RecyclerView的Adapter
        servicesList = new ArrayList<>();
        servicesList.add("我的账户");
        servicesList.add("帮助中心");
        servicesList.add("意见反馈");
        servicesList.add("我的客服");
        servicesList.add("隐私设置");
        servicesList.add("用户协议");
        servicesList.add("关于导盲眼镜");

        adapter = new ServicesAdapter(servicesList);
        recyclerView.setAdapter(adapter);

        return view;
    }

    // Adapter类
    public class ServicesAdapter extends RecyclerView.Adapter<ServicesAdapter.ViewHolder> {
        private List<String> servicesList;

        public ServicesAdapter(List<String> servicesList) {
            this.servicesList = servicesList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.serviceText.setText(servicesList.get(position));
        }

        @Override
        public int getItemCount() {
            return servicesList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView serviceText;

            public ViewHolder(View itemView) {
                super(itemView);
                serviceText = itemView.findViewById(R.id.serviceText);
            }
        }
    }
}
